<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_InfoController extends App_Zend_Controller_Action_Adminb 
{
	

	
	public function init(){
		parent::init();
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL.'/seller');
				
			}else{
			
				$this->_redirect(MY_URL.'/seller-auth-login');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

	}

	
    public function introAction() 
    { 	

		$business=new CBusiness();
		
		$businessArray=$business->getBusinesses();
		
		$this->view->assign('businessArray',$businessArray);
		
		$buyer=new CBuyer();
		
		$buyerArray=$buyer->getUserDetailsUnion(array(R_PRE.'members.uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		$this->view->assign('buyerArray',$buyerArray);
		
		$btype=new CBtype();
		
		$btypeArray=$btype->getBtypes();
		
		$this->view->assign('btypeArray',$btypeArray);
		
		$location=new CFenlei('location');
		
		$location->fenleiLang='en_flname';
    	
		$locationArray=$location->getAllFenlei();
		

    	$selectString=$location->getAllSelect($locationArray,$buyerArray[0]['location_id'],'locationFl.getChildFenlei(this);');
    	
    	$this->view->assign('selectString',$selectString);
    	
    	$address=new CAddress();
		
		$addressArray=$address->getAllAddresses($_COOKIE[COOKIE_PRE.'uid']);		

		$this->view->assign('addressArray',$addressArray);
		
		$bank=new CBank();
		
		$bankArray=$bank->getAllBanks($_COOKIE[COOKIE_PRE.'uid']);

		$this->view->assign('bankArray',$bankArray);
		$this->view->locationArray = $locationArray;
		
    	$member = new CMember();
   		$memberInfo = $member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
   		if ($memberInfo[0]['clubid']) {
   			$matchInfo = new CMatchinfo();
   			$team = $matchInfo->getTeam($memberInfo[0]['clubid']);
   			$this->view->team = $team;   			
   		}
   		$fenlei = new CFenlei('product');
		$clubList = $fenlei->getFenleiByPid(92);
		$this->view->clubList = $clubList;  
    }
    
    public function dointroAction(){
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
    	$buyer=new CBuyer();
    	
    	$counts=$buyer->updateUserDetail(
    					array(
    						'btypeid'=>$thisArray['btypeid'],
    						'company_name'=>$input->company_name,
    						'businessid'=>$thisArray['businessid'],
    						'location_id'=>$thisArray['flid'],
    						'zip'=>$input->zip,
    						'webpage'=>$input->webpage
    					),
    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    				);
    				
    	$member=new CMember();
    	
    	$member->updateUserDetail(array('paypal'=>$input->paypal),array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
    				
    	$address=new CAddress();
    	
    	$addressContent=$input->address;
	
    	if(!empty($addressContent)){
    	
	    	foreach ($addressContent as $value){
	    		
	    		if(trim($value)!=''){
	    		
	    			$address->createAddress(array('uid'=>$_COOKIE[COOKIE_PRE.'uid'],'content'=>$value,'add_time'=>time()));
	    			
	    		}
	    		
	    	}

    	}
    	
    	$bank=new CBank();
    	
    	$bankNameArray=$input->bankName;
    	
    	$bankAccountArray=$input->bankAccount;

    	if(!empty($bankNameArray)){
    	
	    	for($i=0;$i<count($bankNameArray);$i++){
	    		
	    		if(trim($bankAccountArray[$i])!=''){
	    		
	    			$bank->createBank(array('uid'=>$_COOKIE[COOKIE_PRE.'uid'],'bank_name'=>$bankNameArray[$i],'bank_account'=>$bankAccountArray[$i],'add_time'=>time()));
	    			
	    		}
	    		
	    	}

    	}
    	
    	$clubid = intval($this->getRequest()->getParam('clubid'));
   		$member->updateUserDetail(array('clubid'=>$clubid), array('uid'=>$_COOKIE[COOKIE_PRE . 'uid']));
   		$fenlei = new CFenlei('product');
		$fenlei->refreshIntegral();
		
		$counts = $member->updateUserDetail(
    					array(
    						'headimg'=>$input->headimg,
    					),
    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    				);
    				
    	if(is_numeric($counts)){
    		
    		$this->view->msg = 'Edit succeed!';
    		
    	}else{
    		
    		$this->view->msg = $counts;
    		
    	}
    	
    }
    
    public function virtualAction() 
    { 	

    	$member=new CMember();

		$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		$this->view->assign('memberArray',$memberArray);
		
		$virtual=new CVirtual();
		
		$virtual_exchange_rate=$virtual->getDetail('virtual_exchange_rate');
		
		$this->view->assign('virtual_exchange_rate',$virtual_exchange_rate);

    }
    
    public function contactAction() 
    { 	

		$buyer=new CBuyer();
		
		$buyerArray=$buyer->getUserDetailsUnion(array(R_PRE.'buyers.uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		$this->view->assign('buyerArray',$buyerArray);

    }

    public function integralAction()
    {

		$buyer=new CBuyer();

		$buyerArray=$buyer->getUserDetailsUnion(array(R_PRE.'buyers.uid'=>$_COOKIE[COOKIE_PRE.'uid']));

		$this->view->assign('buyerArray',$buyerArray);

    }
    
    public function docontactAction(){    	
        
        
    	if(!$this->getRequest()->isPost()){
			
			$this->view->msg = "error occur!";
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
    	$buyer=new CBuyer();
    	
    	
    	preg_match("/^[A-Za-z0-9_@\-\s]+$/",$thisArray['nick_name'],$matches);
    	if(empty($matches)) {
    		$this->view->msg = "You enter the nick name does not meet the rules!";
			
			return false;
    	}
    	
    	$counts=$buyer->updateUserDetail(
    				   array('nick_name'=>$thisArray['nick_name'],
		    				 'contact_name'=>$thisArray['contact_name'],
		    				 'sex'=>$thisArray['sex'],
		    				 'phone'=>$input->phone,
		    				 'cell'=>$input->cell,
		    				 'fax'=>$input->fax,
		    				 'skype'=>$input->skype,
	    				 ),
    				   array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    				);
    				
    	
    				
    	$member=new CMember();
    	
    	$buyerArray = $buyer->getUserDetailsUnion(array(R_PRE.'buyers.uid'=>$_COOKIE[COOKIE_PRE.'uid']));
    	
    	if (empty($buyerArray[0]['username'])) { //原来没有email
    		if (empty($thisArray['username'])) {
    			$this->view->msg = "Please input your login email!";
				return false;
    		} else {    			
    			$userArray = $member->getUserDetails(array('username'=>$thisArray['username']));
		        if(count($userArray)>0) {
		            $this->view->msg = 'The Login email you entered has been registered by others!';
		            return false;
		        }		        
		        $member->updateUserDetail(
    					array(
    						'username'=>$input->username,
    					),
    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    			);
    		}
    	}
    	
    	$member->updateUserDetail(
    					array(
    						'email'=>$input->email,
    					),
    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    				);
    				
    	if(is_numeric($counts)){
    		
    		$this->view->msg = 'Edit succeed!';
    		
    	}else{
    		
    		$this->view->msg = $counts;
    		
    	}
    	
    	return true;
    	
    }
    
    public function changepassAction() 
    { 	
    	$buyer = new CBuyer();
		$buyerArray = $buyer->getUserDetailsUnion(array(R_PRE.'buyers.uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		$this->view->assign('username',$buyerArray[0]['username']);
    }
    
    public function dochangepassAction(){
        
    	if(!$this->getRequest()->isPost()){
			
			$this->view->msg = "error!";
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);

		if(trim($thisArray['username'])==''||$thisArray['passwordnew']!=$thisArray['passwordnew2']){
			
			$this->view->msg = 'Enter the password twice is not the same!';
			
			return false;
			
		}else{
			
	    	$member=new CMember();
	    	
	    	$userArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])); 
			
			if (empty($userArray[0]['username'])) { //必须填username
	    		if (!empty($thisArray['username'])) {
					$userArray2 = $member->getUserDetails(array('username'=>$thisArray['username']));
			        if(count($userArray2)>0) {
			            $this->view->msg = 'The Login email you entered has been registered by others!';
			            return false;
			        }	
			        $passwordnew=$this->getHelper('user')->encodePassword($input->passwordnew,$thisArray['username'],$userArray[0]['reg_time'],$userArray[0]['reg_ip']);
					$passwordold=$this->getHelper('user')->encodePassword($input->passwordold,$thisArray['username'],$userArray[0]['reg_time'],$userArray[0]['reg_ip']);	        
			        $counts = $member->updateUserDetail(
	    					array(
	    						'username'=>$input->username,
	    						'userpass'=>$passwordnew,
	    						'passwd'=>$input->passwordnew
	    					),
	    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
	    			);
	    		} else {
	    			$this->view->msg = "Please input your login email!";
	    			return false;
	    		}
	    	} else {
	    		$passwordnew=$this->getHelper('user')->encodePassword($input->passwordnew,$userArray[0]['username'],$userArray[0]['reg_time'],$userArray[0]['reg_ip']);
				$passwordold=$this->getHelper('user')->encodePassword($input->passwordold,$userArray[0]['username'],$userArray[0]['reg_time'],$userArray[0]['reg_ip']);	        
				$counts=$member->updateUserDetail(
	    					array(
	    						'userpass'=>$passwordnew,
	    					),
	    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'],'userpass'=>$passwordold)
	    				);
	    	}
			
		}
		
        if($counts>0){
    		
        	setcookie(COOKIE_PRE.'userpass',$passwordnew,time()+3600*24,'/');
        	
        	$sellerSession=new Zend_Session_Namespace('member');

			$sellerSession->userCode=$passwordnew;
        	
			$this->view->msg = 'Successful password modifications!';
    		
    	}else{
    		
    		$this->view->msg = 'The original password error!';
    		
    	}
    	
    	return true;
    	
    }

	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
	
	public function headimgAction()
	{
		$buyer=new CBuyer();
		
		$buyerArray=$buyer->getUserDetailsUnion(array(R_PRE.'buyers.uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		$this->view->assign('buyerArray',$buyerArray[0]);
	}
	
	public function doheadimgAction()
	{
		$this->_helper->viewRenderer->setNoRender();
        
    	if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
		if($input->headimg == '')
			return true;
    				
    	$member=new CMember();
    	
    	$counts = $member->updateUserDetail(
    					array(
    						'headimg'=>$input->headimg,
    					),
    					array('uid'=>$_COOKIE[COOKIE_PRE.'uid'])
    				);
    				
    	if(is_numeric($counts)){
    		
    		$this->view->showMsg('Edit succeed!');
    		
    	}else{
    		
    		$this->view->showMsg($counts);
    		
    	}
    	
    	return true;
	}
    
    //图片处理函数
    public function uploadimgAction(){

    	header("Content-type: text/html; charset=utf-8");

    	$this->getHelper ('layout')->disableLayout ();

    	$upload=new CUpload();

    	$path=new CPath();

    	$thisPath=$path->getPathFromType('temp');

    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');

    	$isSuccess=false;

    	if(is_array($imageArray)){

    		$isSuccess=true;

	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);

	    	$this->view->assign('imgUrl',$imgUrl);

    	}else{

    		$error=$imageArray;

    		$this->view->assign('error',$error);

    	}

    	$this->view->assign('isSuccess',$isSuccess);

    }
    
}
